﻿#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <stack>
using namespace std;

//1926. 迷宫中离入口最近的出口
//https://leetcode.cn/problems/nearest-exit-from-entrance-in-maze/
class Solution {
public:
    int dx[4] = { -1, 1, 0, 0 }, dy[4] = { 0, 0, -1, 1 };
    int nearestExit(vector<vector<char>>& maze, vector<int>& entrance) {
        int m = maze.size(), n = maze[0].size();
        bool vis[m][n];
        memset(vis, false, sizeof vis);
        queue<pair<int, int>> q;
        q.push(make_pair(entrance[0], entrance[1]));
        vis[entrance[0]][entrance[1]] = true;
        int step = 0;
        while (!q.empty()) {
            step++;
            int sz = q.size();
            for (int i = 0; i < sz; i++) {
                auto a = q.front();
                q.pop();
                for (int j = 0; j < 4; j++) {
                    int x = a.first + dx[j], y = a.second + dy[j];
                    if (x >= 0 && x < m && y >= 0 && y < n &&
                        maze[x][y] == '.' && !vis[x][y]) {
                        if (x == 0 || x == m - 1 || y == 0 || y == n - 1)
                            return step;
                        q.push(make_pair(x, y));
                        vis[x][y] = true;
                    }
                }
            }
        }
        return -1;
    }
};

//433. 最小基因变化
//https://leetcode.cn/problems/minimum-genetic-mutation/
class Solution {
public:
    int minMutation(string startGene, string endGene, vector<string>& bank) {
        unordered_set<string> vis; // ⽤来标记已经搜索过的状态
        unordered_set<string> hash(bank.begin(),
            bank.end()); // 存储基因库⾥⾯的字
        string change = "ACGT";

        if (startGene == endGene)
            return 0;
        if (!hash.count(endGene))
            return -1;

        queue<string> q;
        q.push(startGene);
        vis.insert(startGene);

        int ret = 0;
        while (q.size()) {
            ret++;
            int sz = q.size();
            while (sz--) {
                string t = q.front();
                q.pop();
                for (int i = 0; i < 8; i++) {
                    string tmp = t; // 细节问题
                    for (int j = 0; j < 4; j++) {
                        tmp[i] = change[j];
                        if (hash.count(tmp) && !vis.count(tmp)) {
                            if (tmp == endGene)
                                return ret;
                            q.push(tmp);
                            vis.insert(tmp);
                        }
                    }
                }
            }
        }
        return -1;
    }
};

//  127. 单词接龙
// https://leetcode.cn/problems/word-ladder/
class Solution {
public:
    int ladderLength(string beginWord, string endWord,
        vector<string>& wordList) {
        unordered_set<string> hash(wordList.begin(), wordList.end());
        unordered_set<string> vis;
        if (!hash.count(endWord))
            return 0;
        queue<string> q;
        q.push(beginWord);
        vis.insert(beginWord);
        int ret = 1;
        while (q.size()) {
            ret++;
            int sz = q.size();
            while (sz--) {
                string t = q.front();
                q.pop();
                for (int i = 0; i < t.size(); i++) {
                    string tmp = t;
                    for (char ch = 'a'; ch <= 'z'; ch++) {
                        tmp[i] = ch;
                        if (hash.count(tmp) && !vis.count(tmp)) {
                            if (tmp == endWord)
                                return ret;
                            q.push(tmp);
                            vis.insert(tmp);
                        }
                    }
                }
            }
        }
        return 0;
    }
};

//  675. 为高尔夫比赛砍树
// https://leetcode.cn/problems/cut-off-trees-for-golf-event/
class Solution {
public:
    int m, n;
    int cutOffTree(vector<vector<int>>& forest) {
        vector<pair<int, int>> trees;
        m = forest.size(), n = forest[0].size();
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (forest[i][j] > 1)
                    trees.push_back(make_pair(i, j));
            }
        }
        sort(trees.begin(), trees.end(),
            [&](const pair<int, int>& p1, const pair<int, int>& p2) {
                return forest[p1.first][p1.second] <
                    forest[p2.first][p2.second];
            });
        int bx = 0, by = 0, ret = 0;
        for (auto& a : trees) {
            int step = bfs(forest, bx, by, a.first, a.second);
            if (step == -1)
                return -1;
            ret += step;
            bx = a.first, by = a.second;
        }
        return ret;
    }
    vector<vector<bool>> vis;
    int dx[4] = { -1, 1, 0, 0 }, dy[4] = { 0, 0, -1, 1 };
    int bfs(vector<vector<int>> forest, int bx, int by, int ex, int ey) {
        if (bx == ex && by == ey)
            return 0;
        vector<vector<bool>> vis(m, vector<bool>(n, false));
        queue<pair<int, int>> q;
        q.push(make_pair(bx, by));
        vis[bx][by] = true;
        int step = 0;
        while (q.size()) {
            step++;
            int sz = q.size();
            while (sz--) {
                auto a = q.front();
                q.pop();
                for (int i = 0; i < 4; i++) {
                    int x = a.first + dx[i], y = a.second + dy[i];
                    if (x >= 0 && x < m && y >= 0 && y < n && forest[x][y] &&
                        !vis[x][y]) {
                        if (x == ex && y == ey)
                            return step;
                        q.push(make_pair(x, y));
                        vis[x][y] = true;
                    }
                }
            }
        }
        return -1;
    }
};